<?php
/* wwwschool project
Copyright (C) 2009  Phillip Aldridge
Email : info@imi21.com
Web site http://www.imi21.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


/**
 *
 *  wwwschool project
 *  File : WS3_tableview.php
 *  
 *  class WS3_TableView
 *  Generates a table view 
 */  
 
 class WS3_TableView {
 private $width = 0;
 private $CSSElement = 'main';
 private $html = ''; //buffer with the html to display
 private $title = '';
 private $infoRowHtml = '';
  
 private $alternativeRows = false;    
 private $columnTH = array();
 private $column = array();
 private $row = array();  
 private $rowId = array();
 private $columnView =false;
 private $columnViewLink=null;
 private $columnEdit =false;
 private $columnEditLink=null;
 private $columnDelete =false;
 private $columnDeleteLink=null;
 private $columnActive =false;
 private $columnMove =false;
 private $rowToggleLink=null;
 private $rowMoveLink=null;
 private $infoRow =false; 

 
    
   function __construct($css, $w='99%',$a=false) {
    $this->CSSElement=$css; // name of the div element which will be used to display the table
    $this->width=$w; // width of the table
    $this->alternativeRows=$a; // toggles the alternative row colours
   }
   
   function __destruct() {
   }
   
   // set H2 title displayed at the top of the table
   function setTitle($t) {
    $this->title=$t;
   }   
   
   // add a view option for each row
   function addColumn($th='',$link='')
   {
    array_push($this->column,array($th,$link) );
   }
      
  // add a view option for each row
   function addColumnView($t=true,$link='')
   {
    $this->columnView=$t;
    $this->columnViewLink=$link;
   }
   
   // add an edit option  for each row
   function addColumnEdit($t=true,$link='')
   {
    $this->columnEdit=$t;
    $this->columnEditLink=$link;
   }
   
   // add a delete option  for each row
   function addColumnDelete($t=true,$link='')
   {
    $this->columnDelete=$t;
    $this->columnDeleteLink=$link;
   }
   
   // add an activate toggle option
   function addColumnActive($t=true,$link='')
   {
    $this->columnActive=$t;
    $this->rowToggleLink=$link;
   } 
   
   // add an move
   function addColumnMove($t=true,$link='')
   {
    $this->columnMove=$t;
    $this->rowMoveLink=$link;
   }   
   
   
   // add a TH name for display
   function addColumnTH($th) {
    array_push($this->columnTH,$th);
   } 
   
  // text to display at the bottom of the table
    function setInfoRow($t=false) {
    $this->infoRow=$t;
   }
   
  // add text to display at the bottom of the table   
  function addToInfoRow($t='') {
    $this->infoRowHtml.=$t;
   }

  // display rows in alternative colours
  function setAlternativeRows($t=false) {
    $this->alternativeRows=$t;
   }
    
   // add an array of data to a row 
   function addRow($id,$r) {
   $this->row[$id]=$r;
   }

   // add an data to extra column a row 
  /* function addRowColumn($column,$d){
 $this->column['$column']
 }  */
 
   function addRowID($rid='') {
    array_push($this->rowId,$rid);
   }
   
   // display all the data in the table
   private function displayColumns() {
    if (count($this->columnTH)>0)
    {
       $this->html.="<tr>";
       foreach($this->columnTH as $th){
        $this->html.="<th class=\"column$th\">$th</th>";
       }
       
        //display extra columns
       foreach($this->column as $a)
       {
        $this->html.="<th>".$a[0]."</th>";
       }      
       
       if ( $this->columnMove) $this->html.="<th class=\"columnMove\">Move</th>";
       if ( $this->columnActive) $this->html.="<th class=\"columnActive\">Active</th>";
       if ( $this->columnView) $this->html.="<th></th>";
       if ( $this->columnEdit) $this->html.="<th class=\"columnEdit\"></th>";
       if ( $this->columnDelete) $this->html.="<th class=\"columnDelete\"></th>";

       $this->html.="</tr>";    
    }
  } 
   
   private function displayRows() {  
    if (count($this->row)>0)
    {
      $counter=1;
      $alternativeRows=1;
       foreach($this->row as $tr=>$a){
        $this->html.="<tr class=\"tr$alternativeRows\">";
        if (count($tr)>0)
        {
          $i=0;//counter
          $active="";
             foreach($a as $k=>$v){
              if ($k!='active'){
                $this->html.="<td>$v</td>";
                }else // active
                {
                 ($v=='1')? $active='1': $active='0';
                }
              }
            $i++;
            
           }
          foreach($this->column as $a)
          {
            $this->html.="<td><a href=\"javascript:void(0)\"  onclick=\'ajaxCall(\"f=".$a[1]."&amp;id=$tr\")\'\">".$a[0]."</a></td>";
          }          
          // display move up and down column
          if ( $this->columnMove){     
            $this->html.="<td>";    
            if ($counter > 1) $this->html.="<a href=\"javascript:void(0)\" onclick=\'ajaxMove(\"f=".$this->rowMoveLink."&amp;position=up&amp;id=$tr\")\'>Up</a>";
            $this->html.="&nbsp;";
            if ($counter < count($this->row)) $this->html.="<a href=\"javascript:void(0)\" onclick=\'ajaxMove(\"f=".$this->rowMoveLink."&amp;position=down&amp;id=$tr\")\'>Down</a>";
            $this->html.="</td>";
           } 
           
           // display active toggle          
           if ( $this->columnActive){          
           $this->html.="<td><div id=t$tr><a href=\"javascript:void(0)\" onclick=\'ajaxToggle(\"f=".$this->rowToggleLink."&amp;id=$tr\")\'>$active</a></div></td>";
           }  
           
           // display view                
           if ( $this->columnView) $this->html.="<td><a href=\"javascript:void(0)\" onclick=\'ajaxEdit(\"f=".$this->columnViewLink."&amp;id=$tr\")\'>View</a></td>";
           
           // display edit
           if ( $this->columnEdit) $this->html.="<td class=\"columnEdit\"><a href=\"javascript:void(0)\"  onclick=\'ajaxEdit(\"f=".$this->columnEditLink."&amp;id=$tr\")\'>Edit</a></td>";
           
           // display delete
           if ( $this->columnDelete) $this->html.="<td class=\"columnDelete\"><a href=\"javascript:void(0)\" onclick=\'ajaxDelete(\"f=".$this->columnDeleteLink."&amp;id=$tr\")\'>Delete</a></td>";
        

        
        $this->html.="</tr>";
        if ($this->alternativeRows)($alternativeRows==1) ? $alternativeRows=0 : $alternativeRows=1;
        
        $counter++;
       }
     }  
  }
  
 // display the table via ajax  
   function display() {
    if ( $this->title <> '' ) $this->html.="<h2>".$this->title."</h2>";   
     $this->html.="<table width=\"".$this->width."\">";
     $this->displayColumns();
     $this->displayRows();
     $this->html.="</table>";
     if ($this->infoRow) $this->html.=$this->infoRowHtml;
     
    return "document.getElementById('main').innerHTML = '".$this->html."';";
   }
}
?>
